package com.geeksaga.dao.ibatis.mysql;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.geeksaga.common.util.DateConvertor;
import com.geeksaga.dao.TagMapDao;
import com.geeksaga.dao.ibatis.BaseIBatisDao;
import com.geeksaga.domain.Tag;
import com.geeksaga.domain.TagMap;

/**
 * @author newbie
 * @since 0.1
 */
@Repository
public class TagMapDaoImpl extends BaseIBatisDao implements TagMapDao {

    /* (non-Javadoc)
     * @see com.geeksaga.dao.TagMapDao#add(com.geeksaga.domain.TagMap)
     */
    public TagMap add(TagMap tagMap) throws DataAccessException {
        tagMap.setRegistTimestamp(DateConvertor.getDateTimeFormat());
        
        return (TagMap) getSqlMapClientTemplate().insert("TAG_MAP.add", tagMap);
    }

    /* (non-Javadoc)
     * @see com.geeksaga.dao.TagMapDao#findList(com.geeksaga.domain.TagMap)
     */
    @SuppressWarnings("unchecked")
    public List<Tag> findList(TagMap tagMap) throws DataAccessException {
        return (List<Tag>) getSqlMapClientTemplate().queryForList("TAG_MAP.findList", tagMap);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.dao.TagMapDao#exists(com.geeksaga.domain.TagMap)
     */
    @SuppressWarnings("unchecked")
    public boolean exists(TagMap tagMap) throws DataAccessException {
        List<TagMap> list = (List<TagMap>) getSqlMapClientTemplate().queryForList("TAG_MAP.findByBbsTag", tagMap);
        
        if(list == null || (list != null && list.size() == 0))
            return false;
        else
            return true;
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.dao.TagMapDao#remove(com.geeksaga.domain.TagMap)
     */
    public int remove(TagMap tagMap) throws DataAccessException {
        return getSqlMapClientTemplate().delete("TAG_MAP.remove", tagMap);
    }
}